﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using SoSlam.Common.Communication.GameMessages;
using Microsoft.Xna.Framework.Graphics;

namespace SoSlam.Common.ClientLogic
{
    public class Background : DrawableGameComponent
    {
        private World _world;
        private TrackingCamera _camera;

        private SpriteBatch _spriteBatch;

        private Texture2D _skyTexture;
        private Texture2D _grassSprite;
        private Texture2D _treeSprite;

        private List<Cloud> _clouds = new List<Cloud>();
        private TimeSpan _cloudSpawnSpan = TimeSpan.FromSeconds(3);
        private TimeSpan _lastCloudSpawn = new TimeSpan();

        public Background(Game game, World world, TrackingCamera camera)
            : base(game)
        {
            _world = world;
            _camera = camera;
        }

        public override void Initialize()
        {
            this.DrawOrder = 100;
            
            _spriteBatch = new SpriteBatch(Game.GraphicsDevice);

            var signs = new Signs(Game, _camera);
            signs.DrawOrder = 102;
            Game.Components.Add(signs);

            base.Initialize();
        }

        protected override void LoadContent()
        {
            _skyTexture = Game.Content.Load<Texture2D>("Sprites/sky_gradient");

            _grassSprite = Game.Content.Load<Texture2D>("Sprites/grass2");
            _treeSprite = Game.Content.Load<Texture2D>("Sprites/tree");

            base.LoadContent();
        }

        public override void Update(GameTime gameTime)
        {
            // Spawns a new cloud every 1 to 4 seconds
            if ((gameTime.TotalGameTime - _lastCloudSpawn) > _cloudSpawnSpan)
            {
                _cloudSpawnSpan = TimeSpan.FromSeconds(World.Random.Next(2, 4));
                _lastCloudSpawn = gameTime.TotalGameTime;

                Cloud cloud = new Cloud(Game, _world, _camera);
                cloud.DrawOrder = 101;
                Game.Components.Add(cloud);
                _clouds.Add(cloud);
            }

            // Free the non visible clouds
            for (int i = 0; i < _clouds.Count; i++)
            {
                var cloud = _clouds[i];

                if (cloud.Position.X + cloud.Width + 20 < 0)
                {
                    Game.Components.Remove(cloud);
                    _clouds.Remove(cloud);
                }
            }

            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            _spriteBatch.Begin();

            _spriteBatch.Draw(_skyTexture, 
                new Rectangle(0, 0, _camera.Fov.Width, _camera.Fov.Height),
                new Rectangle(0, (int)(_skyTexture.Height + _skyTexture.Height * (_camera.Fov.Height + (int)_camera.Position.Y - 20) / 3000f), 3, 50), 
                Color.White);

            // Grass
            {
                for (int i = 0; i <= Math.Ceiling((float)_camera.Fov.Width / _grassSprite.Width); i++)
                {
                    int offset = Convert.ToInt32(Math.Floor(_camera.Position.X / _grassSprite.Width) * _grassSprite.Width);
                    var position = new Vector2(offset + i * _grassSprite.Width, -_grassSprite.Height + 20) - _camera.Position;
                    _spriteBatch.Draw(_grassSprite, position, Color.White);
                }
            }

            //Tree
            {
                int offset = Convert.ToInt32(_treeSprite.Width / 2);
                var position = new Vector2(-offset - 78, -_treeSprite.Height - 20) - _camera.Position;
                _spriteBatch.Draw(_treeSprite, position, Color.White);
            }

            _spriteBatch.End();

            base.Draw(gameTime);
        }
    }
}
